package com.ybzl.gateway.controller;

import com.ybzl.gateway.domain.Beacon;
import com.ybzl.gateway.domain.dto.BeaconDTO;
import com.ybzl.gateway.domain.Result;
import com.ybzl.gateway.domain.dto.FenceDTO;
import com.ybzl.gateway.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("beacon/")
@Slf4j
public class BeaconController {

    @Autowired
    private IDeviceService deviceService;

    @GetMapping("list")
    public Result list() {
        List<Beacon> list = deviceService.selectBeacon();
        return new Result(200, list, true);
    }

    @PostMapping("add")
    public Result add(@RequestBody BeaconDTO beaconDTO) {
        try {
            deviceService.saveOrUpdate(beaconDTO.getBeaconId(), null, beaconDTO.getBeaconX(), beaconDTO.getBeaconY());
            return new Result(200, null, true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400, e.getMessage(), false);
        }

    }

    @PostMapping("del")
    public Result del(@RequestBody BeaconDTO beaconDTO) {
        int flag = deviceService.delBeaconByTs(beaconDTO.getBeaconId());
        if (flag != 0) {
            return new Result(200, null, true);
        } else {
            return new Result(400, null, false);
        }
    }


}
